using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SqlCeViewer.Base;

namespace SqlCeViewer.UI
{
    public partial class CreateEmptyDb : BaseSubForm
    {
        public delegate void Del_CreateDbAfterSuccesful(string fullPath, string pwd);
        public Del_CreateDbAfterSuccesful CreateDbAfterSuccesful;

        public CreateEmptyDb()
        {
            InitializeComponent();
            txtPwd.Properties.PasswordChar=txtConfirmPwd.Properties.PasswordChar = '*';
        }

        private void butCreate_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtDbPath.Text))
            {
                MessageBox.Show("Please input db name");
                return;
            }
            if (txtPwd.Text!= txtConfirmPwd.Text)
            {
                MessageBox.Show("Two passwords are not same,please try again");
                return;
            }

            string dbName = txtDbPath.Text;
            string pwd = txtPwd.Text;
            bool isEncrypted = (bool)this.checkEdit1.Checked;

            if (GlobalInfo.MainEngineer.CreateDataBase(dbName, pwd, isEncrypted))
            {
                MessageBox.Show(string.Format("Create {0} successful", dbName));
                if (CreateDbAfterSuccesful != null)
                {
                    CreateDbAfterSuccesful(dbName, pwd);
                }

                Dispose();
            }
        }

        private void txtDbPath_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            SaveFileDialog sf = new SaveFileDialog();
            sf.Filter = GlobalInfo.SQLCE_FILE_FILTER;
            if (sf.ShowDialog() == DialogResult.OK)
            {
                txtDbPath.Text = sf.FileName;
            }
        }
    }
}